import sys
import os
import getopt
import traceback
import ak_stock_fund as ak
import ak_em_hsgt as ake
import ak_em_comment as akc
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import get_stock_data_path as sp
import dfcf_focus_rank as fr
import dfcf_top_board as tb
from matplotlib.font_manager import FontProperties


from datetime import datetime

def get_disp_value(v : float)->str:
   if(v >= 100000000.0 or v <= -100000000.0):
     return "%.2f"%float(v/100000000) + "亿"
   elif(v>= 10000.0 or v<= -10000.0):
     return "%.1f"%float(v/10000) + "万"
   else:
     return str(v)
#pd.DataFrame(columns=['A', 'B', 'C'], index=[0,1,2])
#Columns = ['代码','简称','类型','信号']
#df = pd.DataFrame(columns = Columns)

stock_path = sp.get_stock_data_path()
png_path = sp.get_stock_png_path()

now = datetime.now()
datestr = datetime.strftime(now,'%Y-%m-%d')
datetimestr = datetime.strftime(now,'%Y-%m-%d-%H-%M')


fundflow_csv = stock_path + sp.get_separator() + "dm" + sp.get_separator() + datestr + "_f.csv"
fundflow_png = png_path + sp.get_separator() + datestr +"_fundflow.png"

pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
pd.set_option('display.max_rows', None)

# real time stock quotes
# stock_zh_a_spot_df = ak.stock_zh_a_spot()
# print(stock_zh_a_spot_df)

# history data    stock_zh_a_daily
# stock_zh_a_daily_hfq_df = ak.stock_zh_a_daily(symbol="sz000063", adjust="qfq")

# stock_zh_index_spot  实时指数行情数据

# 获取东方财富网-数据中心-研究报告-东方财富分析师指数-东方财富分析师指数2020最新排行
# stock_em_analyst_rank_df = ak.stock_em_analyst_rank()
# print(stock_em_analyst_rank_df)

# indicator="今日"; {"今日", "3日", "5日", "10日"}  资金流向
def get_fund_flow_rank():
  fundflow_csv = stock_path + sp.get_separator() + "dm" + sp.get_separator() + datestr + "_f.csv"
  fundflow_png = png_path + sp.get_separator() + datestr +"_fundflow.png" 
  stock_individual_fund_flow_df = ak.stock_individual_fund_flow(stock="000001", market="sz")
  lastdate = stock_individual_fund_flow_df["日期"][stock_individual_fund_flow_df.shape[0]-1]
  print(lastdate)
  sc="000001"

  csv_name =  stock_path + sp.get_separator() + "m" +  sp.get_separator() + sc + "_f.csv"
  if not os.path.exists(csv_name):
    print(csv_name + " not exist!")
    return
  df = pd.read_csv(csv_name)
  lastdate_csv = df["日期"][df.shape[0]-1]
  if(lastdate == lastdate_csv):
    print("the latest date of " + sc + " already is " + lastdate +",no need to update")
    return

  stock_individual_fund_flow_rank_df = ak.stock_individual_fund_flow_rank(indicator="今日")
  cashintoday = stock_individual_fund_flow_rank_df.head(5000)[['代码', '名称', '最新价', '涨跌幅', '主力净流入-净额', '主力净流入-净占比','超大单净流入-净额', 
  '超大单净流入-净占比','大单净流入-净额', '大单净流入-净占比','中单净流入-净额', '中单净流入-净占比','小单净流入-净额', '小单净流入-净占比']]
  #cashin3 = ak.stock_individual_fund_flow_rank(indicator="3日").head(70)[
   #   ['代码', '名称', '最新价', '涨跌幅', '主力净流入-净额', '主力净流入-净占比']]
  #cashin5 = ak.stock_individual_fund_flow_rank(indicator="5日").head(70)[
  #    ['代码', '名称', '最新价', '涨跌幅', '主力净流入-净额', '主力净流入-净占比']]
  
  #print(pd.merge(pd.merge(cashintoday, cashin3), cashin5))
  #with open(fundflow_txt,"wt") as f:
  #  print(cashintoday,file = f)
  #  f.close()
  
  cashintoday.to_csv(fundflow_csv,index=False) 
 
  col_name=['代码', '名称','主力净流入-净额','小单净流入-净额','中单净流入-净额','大单净流入-净额','超大单净流入-净额','主力净流入-净占比',
  '小单净流入-净占比','中单净流入-净占比','大单净流入-净占比','超大单净流入-净占比','最新价','涨跌幅']
  tmp=cashintoday.reindex(columns=col_name)
  for i in range(cashintoday.shape[0]):
    line = tmp.loc[i]  
    sc = line[0]
    if(sc[0] == '2' or sc[0] == '9'):
      print(sc + " ignore")
      continue
    content = line[2:]
    x = content.to_string(header=False,
                    index=False).split('\n')
    a=datestr + ',' + ','.join(v.strip() for v in x)
    csv_name =  stock_path + sp.get_separator() + "m" +  sp.get_separator() + sc + "_f.csv"
    wh = False
    if not os.path.exists(csv_name):
       wh = True
       print(sc + ",file not exist,create it")
    with open(csv_name,"a",encoding = "utf-8") as file:
      if(wh):
        file.write('日期,主力净流入-净额,小单净流入-净额,中单净流入-净额,大单净流入-净额,超大单净流入-净额,主力净流入-净占比,小单净流入-净占比,中单净流入-净占比,大单净流入-净占比,超大单净流入-净占比,收盘价,涨跌幅\n')
      file.write(a + "\n")
      file.close()
 

def draw_fund_flow_rank_table():
  if not os.path.exists(fundflow_csv):
    print(fundflow_csv + " not exist,draw draw_fund_flow_rank_table fail!")
    return
  cashintoday = pd.read_csv(fundflow_csv,dtype={"代码":str})
  print(cashintoday.index)
  print(cashintoday.index.start)
  print(cashintoday.index.stop)
  print(cashintoday.index.step)
  print(cashintoday.columns)
  print(cashintoday.columns.size)
  m = cashintoday.values
  print(m[2][2])
  colors = plt.cm.Reds(np.linspace(0.8, 0.4, 20))
  c = cashintoday.head(20)
  #fig = plt.figure(1,1,figsize=(10, 10))
  fig = plt.subplots(1,1,figsize=(10, 7.5))
  plt.subplots_adjust(left=0.02,right=0.999,bottom=0,top=0.96)
  
  font = {'family' : 'SimSun','weight' : 'bold','size' : 10}
  plt.rc('font', **font)
  header=['代码', '名称', '最新价', '涨跌幅', '流入净额', '净占比','超大单', '净占比','大单', '净占比','中单', '净占比','小单', '净占比']
  vf = np.zeros((20,14),dtype=int)
  cstr = [[0 for i in range(14)] for i in range(20)] 
  print(type(cstr[0][0]))
  for col in range(14):
      for row in range(20):
         #print("%d,%d,%s"%(col,row,type(c.values[row][col])))
         
         if(col == 3):#涨跌幅
            if(c.values[row][col] == '-'):
              v = 0
              cstr[row][col] = '-'
            else:
              v = float(c.values[row][col])
              cstr[row][col] = str(c.values[row][col])
              cstr[row][col] += "%"
            if(v<0.0):
              vf[row][col] = 0
              vf[row][col-1] = 0
            else:
              vf[row][col] = 1
              vf[row][col-1] = 1 
         elif(col >= 5 and col%2 ==1):
           if(c.values[row][col] == '-'):
              v = 0
              cstr[row][col] = '-'
           else:
              v = float(c.values[row][col])
              cstr[row][col] = str(c.values[row][col])
              cstr[row][col] += "%"
           if(c.values[row][col-1] == '-'):
             cstr[row][col-1] = '-'
           else:
             cstr[row][col-1] = get_disp_value(float(c.values[row][col-1]))
           if(v<0.0):
              vf[row][col] = 0
              vf[row][col-1]=0
           else:
              vf[row][col] = 1
              vf[row][col-1] = 1
         elif(col <3):
           cstr[row][col] = str(c.values[row][col])
  
  plt.text(0.3,0.99,datestr + "主力资金净流入排名", fontsize=15, color='r')
  
  the_table = plt.table(cellText=cstr,
                        rowLabels=range(1, 21),
                        rowColours=colors,
                        colLabels=header,
                        cellLoc = 'center',colLoc='center',loc = 'center')
  the_table.auto_set_font_size(False)
  the_table.set_fontsize(13)
  
      
  for (row, col), cell in the_table.get_celld().items():
      if (row == 0):
          cell.set_text_props(fontproperties=FontProperties(weight='bold', size=12))
      else:
          cell.set_text_props(fontproperties=FontProperties(weight='bold', size=12))
      cell.set_height(1/22)
      if( col >= 2 and row >=1):
         if(vf[row-1][col] == 1):
            cell.get_text().set_color((0.8, 0, 0))
         else:
            cell.get_text().set_color((0, 0.4, 0))
      elif(col == 0 or col ==1):
         cell.get_text().set_color((0.1, 0.2, 0.5))
  plt.axis('off')
  #plt.show()   
  
  
  plt.savefig(fundflow_png)   




"""
#stock_individual_fund_flow_df = ak.stock_individual_fund_flow(stock="600094", market="sh")
#print(stock_individual_fund_flow_df)
"""
"""
with open("all_a_code.EBK", "rt") as f:
    for line in f.readlines():
        line = line.strip('\n')  #去掉列表中每一个元素的换行符
        if(len(line) <6):
          continue
        sc = line[1:]
        sm = "sh"
        if(line[0] == '0'):
          sm = "sz"
        csv_name =  stock_path + sp.get_separator() + "m" +  sp.get_separator() + sc + "_f.csv";
        if os.path.exists(csv_name):
          continue
        stock_individual_fund_flow_df = ak.stock_individual_fund_flow(stock=sc, market=sm)
        if(stock_individual_fund_flow_df is None):
          continue
        stock_individual_fund_flow_df.to_csv(csv_name,index=False)
        print(line)
"""
#print(stock_individual_fund_flow_df)

def get_stock_north_history(sc:str):
  csv_name =  stock_path + sp.get_separator() + "n3" +  sp.get_separator() + sc + "_n.csv";
  if os.path.exists(csv_name):
    print(csv_name +"already exist,ignore!")
    return
  try:
    stock_individual_fund_flow_df = ake.stock_em_hsgt_individual_stock_statistics(sc,"2020-01-05",datestr)
  except Exception as e:
    print (e)
    traceback.print_exc()
  if(stock_individual_fund_flow_df is None):
    return
  stock_individual_fund_flow_df.sort_values(by='持股日期',axis = 0, ascending=True,inplace = True)
  stock_individual_fund_flow_df.to_csv(csv_name,index=False)

"""
#获取个股北向资金数据
with open("all_a_code.EBK", "rt") as f:
    for line in f.readlines():
        line = line.strip('\n')  #去掉列表中每一个元素的换行符
        if(len(line) <6):
          continue
        sc = line[1:]

        csv_name =  stock_path + sp.get_separator() + "n" +  sp.get_separator() + sc + "_n.csv";
        if os.path.exists(csv_name):
          continue
        try:
          stock_individual_fund_flow_df = ake.stock_em_hsgt_individual_stock_statistics(sc,"2020-01-05",datestr)
        except Exception as e:
          print (e)
          traceback.print_exc()
        if(stock_individual_fund_flow_df is None):
          continue
        stock_individual_fund_flow_df.sort_values(by='持股日期',axis = 0, ascending=True,inplace = True)
        stock_individual_fund_flow_df.to_csv(csv_name,index=False)
        
        print(line)
"""

"""
#北向数据文件删除几列
with open("all_a_code.EBK", "rt") as f:
    for line in f.readlines():
        line = line.strip('\n')  #去掉列表中每一个元素的换行符
        if(len(line) <6):
          continue
        sc = line[1:]

        csv_name =  stock_path + sp.get_separator() + "n" +  sp.get_separator() + sc + "_n.csv";
        if not os.path.exists(csv_name):
          continue
        stock_individual_fund_flow_df = pd.read_csv(csv_name,dtype={"股票代码":str})
        stock_individual_fund_flow_df = stock_individual_fund_flow_df[[
            '持股日期',
            '股票代码',
            '股票简称',
            '当日收盘价',
            '当日涨跌幅',
            '持股数量',
            '持股数量占发行股百分比'
        ]]
        stock_individual_fund_flow_df.to_csv(csv_name,index=False)
        
        print(line)
"""
#本地北向数据与最新北向数据合并
"""
def merge_north_individual():
  with open("all_a_code.EBK", "rt") as f:
      for line in f.readlines():
          line = line.strip('\n')  #去掉列表中每一个元素的换行符
          if(len(line) <6):
            continue
          sc = line[1:]
  
          csv_name =  stock_path + sp.get_separator() + "n" +  sp.get_separator() + sc + "_n.csv"
          if not os.path.exists(csv_name):
            continue
          csv_name_out =  stock_path + sp.get_separator() + "n" +  sp.get_separator() + sc + "_n.csv"
          if os.path.exists(csv_name_out):
            continue
          stock_individual_fund_flow_df = pd.read_csv(csv_name,dtype={"股票代码":str})
          stock_individual_fund_flow_df = stock_individual_fund_flow_df[[
              '持股日期',
              '股票代码',
              '股票简称',
              '当日收盘价',
              '当日涨跌幅',
              '持股数量'
          ]]
          lastdate = stock_individual_fund_flow_df['持股日期'][stock_individual_fund_flow_df.shape[0]-1]
          stock_online_fund_flow_df = ake.stock_em_hsgt_individual_stock_statistics(sc, lastdate, datestr)
          if(stock_online_fund_flow_df is None):
            print("get "+ sc + "  error")
            continue
          stock_online_fund_flow_df = stock_online_fund_flow_df[[
              '持股日期',
              '股票代码',
              '股票简称',
              '当日收盘价',
              '当日涨跌幅',
              '持股数量'
          ]]
          stock_online_fund_flow_df.sort_values(by='持股日期', axis = 0, ascending=True,inplace = True)
          stock_individual_fund_flow_df = stock_individual_fund_flow_df.append(stock_online_fund_flow_df[stock_online_fund_flow_df['持股日期']>lastdate])
          
          stock_individual_fund_flow_df.to_csv(csv_name_out,index=False)
"""
def get_all_north_individual():
    nf_df = ake.stock_em_hsgt_stock_statistics("北向持股","2021-03-05",datestr)
    nf_df.sort_values(by=['持股日期'], axis = 0, ascending=True,inplace = True)
    nf_df = nf_df.reset_index(drop=True)
    scode_df = nf_df['股票代码']
    for i in range(scode_df.shape[0]):
      sc = scode_df[i]
      print(sc)
      get_north_individual(sc)

def get_north_individual(sc:str):
  lastdate="2021-01-01"
  stock_online_fund_flow_df = ake.stock_em_hsgt_individual_stock_statistics(sc, lastdate, datestr)
  if(stock_online_fund_flow_df is None):
    print("get "+ sc + "  error")
    return
  stock_online_fund_flow_df.sort_values(by=['持股日期'], axis = 0, ascending=True,inplace = True)
  stock_online_fund_flow_df = stock_online_fund_flow_df.reset_index(drop=True)
  nf_df = stock_online_fund_flow_df[[
          '持股日期',
          '股票代码',
          '股票简称',
          '当日收盘价',
          '当日涨跌幅',
          '持股数量'
  ]]
  csv_name =  stock_path + sp.get_separator() + sc + "_n.csv"
  nf_df.to_csv(csv_name,index = False)


def remake_north_daily_rank(nf:str,of:str):
  csv_name_n =  stock_path + sp.get_separator() + "dn" +  sp.get_separator() + nf
  if not os.path.exists(csv_name_n):
    print(csv_name_n + " not exist!")
    return
  csv_name_o =  stock_path + sp.get_separator() + "dn" +  sp.get_separator() + of
  if not os.path.exists(csv_name_o):
    print(csv_name_o + " not exist!")
    return
  dfn = pd.read_csv(csv_name_n,dtype={"股票代码":str})
  dfo = pd.read_csv(csv_name_o,dtype={"股票代码":str})
  scode_df = dfn['股票代码']
  for i in range(scode_df.shape[0]):
    sc = scode_df[i]
    share_hold = dfn.iloc[i, 4]
    last_record = dfo[dfo.股票代码 == sc]
    if(last_record.shape[0]==1):
      share_hold_last = last_record.iloc[0,4]
      share_hold_change = share_hold - share_hold_last
      dfn.iloc[i, 5] = share_hold_change
      dfn.iloc[i, 6] = dfn.iloc[i, 5] * dfn.iloc[i, 2]
      last_cont_increase = last_record.iloc[0,7]
      if(share_hold_change > 0):
        if(last_cont_increase>=0):
          dfn.iloc[i, 6] = last_cont_increase + 1
        else:
          dfn.iloc[i, 6] = 1
      else:
        if(last_cont_increase<=0):
          dfn.iloc[i, 6] = last_cont_increase - 1
        else:
          dfn.iloc[i, 6] = -1
  dfn.to_csv(csv_name_n,index = False)

def merge_north_individual():
  sc="000001"
  lastdate="2021-02-23"
  df_stock_basic = None
  csv_name =  stock_path + sp.get_separator() + "n" +  sp.get_separator() + sc + "_n.csv"
  if not os.path.exists(csv_name):
    print(csv_name + " not exist!")
    return
  df = pd.read_csv(csv_name,dtype={"股票代码":str})

  lastdate = df["持股日期"][df.shape[0]-1]
  print("last date is " + lastdate)
  stock_online_fund_flow_df = ake.stock_em_hsgt_individual_stock_statistics(sc, lastdate, datestr)
  if(stock_online_fund_flow_df is None):
    print("get "+ sc + "  error")
    return
  stock_online_fund_flow_df.sort_values(by=['持股日期'], axis = 0, ascending=True,inplace = True)
  stock_online_fund_flow_df = stock_online_fund_flow_df.reset_index(drop=True)
  all_date = stock_online_fund_flow_df["持股日期"]

  dn_csv = stock_path + sp.get_separator() + "dn" +  sp.get_separator() + lastdate + "_n.csv"
  if not os.path.exists(dn_csv):
    print(dn_csv + "not exist,please make it at first,maybe use make_daily_northrank()")
    return
  dn_df = pd.read_csv(dn_csv,dtype={"股票代码":str})
  for j in range(stock_online_fund_flow_df.shape[0]):
    date = all_date[j]
    if(date == lastdate):
      continue
    nf_df = ake.stock_em_hsgt_stock_statistics("北向持股",date,date)
    nf_df = nf_df[[
          '持股日期',
          '股票代码',
          '股票简称',
          '当日收盘价',
          '当日涨跌幅',
          '持股数量'
        ]]
    scode_df = nf_df['股票代码']
    for i in range(scode_df.shape[0]):
      sc = scode_df[i]
      if(sc[0] == '2' or sc[0] == '9'):
        print(sc + " ignore")
        continue
      print(sc)
      if(len(nf_df['股票简称'][i])>10):
        if(df_stock_basic is None):
          stock_basic_csv = stock_path + sp.get_separator() + "stock_basic.csv"
          df_stock_basic = pd.read_csv(stock_basic_csv, dtype={"symbol":str})
        if(not df_stock_basic is None):
          symbol = df_stock_basic['name'][df_stock_basic[df_stock_basic.symbol == sc].index[0]]
          nf_df['股票简称'][i] = symbol

      dn_index = dn_df[dn_df.股票代码 == sc].index
      dn_content = nf_df.loc[i].to_list()
      dn_content.append(0)
      dn_content.append(0)
      dn_content.append(0)
      if(dn_index.size == 0):
        row = []
        for ic in range(dn_df.shape[1]):
          v = {}
          v[dn_df.columns[ic]] = dn_content[ic+1]
          row.append(v)
        dn_df = dn_df.append(row,ignore_index=True)
      
      else:
        index = dn_index[0]
        last = dn_df.loc[index]
        if(str(dn_content[5]) == '-' or str(last[5]) == '-' or str(dn_content[3])=='-'):
          dn_content[6] = 0
          dn_content[7] = 0
        else:
          dn_content[6] = dn_content[5] - last[4]
          dn_content[7] = dn_content[6] * dn_content[3]
          if(dn_content[5] > last[4]):
            if(last[7] > 0):
              dn_content[8] = last[7] + 1
            else:
              dn_content[8] = 1
          elif(dn_content[5] < last[4]):
            if(last[7] < 0):
              dn_content[8] = last[7] - 1
            else:
              dn_content[8] = -1
          else:
            dn_content[8] = 0
        for ic in range(len(dn_content)-1):
          dn_df.iloc[index, ic] = dn_content[ic+1]

      """#转换原有文件，只保留持股数量。由于已经转换了，就不再转换
      csv_name =  stock_path + sp.get_separator() + "n" +  sp.get_separator() + sc + "_n.csv"
      if os.path.exists(csv_name) and j==1:
        stock_individual_fund_flow_df = pd.read_csv(csv_name,dtype={"股票代码":str})
        print(csv_name)
        stock_individual_fund_flow_df = stock_individual_fund_flow_df[[
          '持股日期',
          '股票代码',
          '股票简称',
          '当日收盘价',
          '当日涨跌幅',
          '持股数量'
        ]]
        stock_individual_fund_flow_df.to_csv(csv_name,index = False)
      """
      content = nf_df.loc[i]
      #col =['持股日期','股票代码','股票简称','当日收盘价','当日涨跌幅', '持股数量','持股数量占发行股百分比          

      x = content.to_string(header=False,
                      index=False).split('\n')
      a=','.join(v.strip() for v in x)
      csv_name =  stock_path + sp.get_separator() + "n" +  sp.get_separator() + sc + "_n.csv"
      wh = False
      if not os.path.exists(csv_name):
          print(csv_name + " not exist,create it")
          Wh = True          
      with open(csv_name,"a",encoding="utf-8") as file:
        if(wh == True):
          file.write("持股日期,股票代码,股票简称,当日收盘价,当日涨跌幅,持股数量\n")
        if(a.endswith(".0")):
          a=a[:len(a) - 2]
        file.write(a + "\n")
        file.close()
    
    csv_name = stock_path + sp.get_separator() + "dn" +  sp.get_separator() + date + "_n.csv"
    dn_df.to_csv(csv_name,index = False)



    #stock_individual_fund_flow_df.to_csv(csv_name,index=False)
    """
      sc="000001"
      stock_individual_fund_flow_df = ake.stock_em_hsgt_individual_stock_statistics(sc, "2021-02-23", "2021-03-15"            
      print(stock_individual_fund_flow_df.shape[0])
      print(stock_individual_fund_flow_df.shape[1])
      print(stock_individual_fund_flow_df            
      stock_em_hsgt_stock_statistics_df = ake.stock_em_hsgt_stock_statistics(
          symbol="北向持股", start_date="20210227", end_date="20210307"
      )
      print(stock_em_hsgt_stock_statistics_df)
             
      print(line)
    """
def make_daily_northrank():
  col_name =   ['持股日期',
          '股票代码',
          '股票简称',
          '当日收盘价',
          '当日涨跌幅',
          '持股数量',
          '单日增持',
          '单日增持市值',
          '连续增持' ]
  
  all_df = None
  import glob
  filename =  stock_path + sp.get_separator() + "n" +  sp.get_separator() + "*_n.csv"
  for csv_name in glob.glob(filename):
    print("read "+ csv_name)
    tmp = pd.read_csv(csv_name,encoding="utf-8",dtype={"股票代码":str})
    tmp=tmp.reindex(columns=col_name)
    nRow = tmp.shape[0]
    tmp['单日增持'][0] = 0
    tmp['单日增持市值'][0] = 0
    tmp['连续增持'][0] = 0
    for i in range(1,nRow):
      if(tmp['当日收盘价'][i]=='-'):
        tmp['单日增持'][i] = 0
        tmp['单日增持市值'][i] = 0
        tmp['连续增持'][i] = 0
        continue

      tmp['单日增持'][i] = tmp['持股数量'][i] - tmp['持股数量'][i-1]
      tmp['单日增持市值'][i] = tmp['单日增持'][i]*tmp['当日收盘价'][i]
      if(tmp['单日增持'][i] > 0):
        if(tmp['单日增持'][i-1]  > 0):
          tmp['连续增持'][i] = tmp['连续增持'][i-1]  + 1
        else:
          tmp['连续增持'][i] = 1
      elif(tmp['单日增持'][i] == 0):
        tmp['连续增持'][0] = 0
      else:
        if(tmp['单日增持'][i-1]  >= 0):
          tmp['连续增持'][i] = -1
        else:
          tmp['连续增持'][i] = tmp['连续增持'][i-1] - 1

    if(all_df is None):
      all_df = tmp
    else:
      all_df = all_df.append(tmp)
   
  all_day = all_df["持股日期"].unique()
  for i in range(all_day.shape[0]):
    ds = all_day[i]
    if(str(ds)<="2021-03-05"):
      continue
    tmp_df = all_df[all_df["持股日期"].isin([ds])]
    #tmp_df = tmp_df.sort_values(by = ["主力净流入-净额"],ascending=False)
    del tmp_df["持股日期"]
    csv_name =  stock_path + sp.get_separator() + "dn" +  sp.get_separator() + str(ds) + "_n.csv"
    tmp_df.to_csv(csv_name,index=False)
    print(ds)
"""
#转换个股主力资金流数据csv文件，去掉index列
with open("all_a_code.EBK", "rt") as f:
    for line in f.readlines():
        line = line.strip('\n')  #去掉列表中每一个元素的换行符
        if(len(line) <6):
          continue
        sc = line[1:]
        csv_name =  stock_path + sp.get_separator() + "m" +  sp.get_separator() + sc + "_fundflow.csv"
        if os.path.exists(csv_name):
          stock_individual_fund_flow_df = pd.read_csv(csv_name)
          if(stock_individual_fund_flow_df is None):
             continue
          del stock_individual_fund_flow_df[stock_individual_fund_flow_df.columns[0]]
          csv_name =  stock_path + sp.get_separator() + "m" +  sp.get_separator() + sc + "_f.csv"
          stock_individual_fund_flow_df.to_csv(csv_name,index=False)
          print(line)
"""
#根据个股主力资金流数据文件，生成每日排名数据文件
def make_daily_fundflow():
  csv_name =  stock_path + sp.get_separator() + "2021-03-03_f.csv"
  temp_df = pd.read_csv(csv_name)
  #del temp_df[temp_df.columns[0]]
  
  col_name=temp_df.columns.tolist() 
  col_name.insert(0,"日期")
  col_name[3]="收盘价"
  all_df = None
  for i in range(temp_df.shape[0]):
    sc = str(temp_df["代码"][i]).zfill(6)
    print(temp_df["名称"][i])
    sn = temp_df["名称"][i]
    print(sc)
    print(sn)
    csv_name =  stock_path + sp.get_separator() + "m" +  sp.get_separator() + sc + "_f.csv"
    if not os.path.exists(csv_name):
      continue
    print("read "+ csv_name)
    tmp = pd.read_csv(csv_name,encoding="utf-8")
    tmp=tmp.reindex(columns=col_name)
    tmp["代码"] = tmp["代码"].astype(str)
    for j in range(tmp.shape[0]):
      tmp["代码"][j]=sc
      tmp["名称"][j]=sn

    if(all_df is None):
      all_df = tmp[tmp["日期"]>"2021-03-05"]
    else:
      all_df = all_df.append(tmp[tmp["日期"]>"2021-03-05"])
  """    
    if(all_df is None):
      all_df = tmp
    else:
      all_df = all_df.append(tmp)
  """ 
  #  if( i == 4):
  #    break
  
  
  all_day = all_df["日期"].unique()
  for i in range(all_day.shape[0]):
    ds = all_day[i]
    tmp_df = all_df[all_df["日期"].isin([ds])]
    #tmp_df = tmp_df.sort_values(by = ["主力净流入-净额"],ascending=False)
    del tmp_df["日期"]
    csv_name =  stock_path + sp.get_separator() + "dm" +  sp.get_separator() + ds + "_f.csv"
    tmp_df.to_csv(csv_name,index=False)
    print(ds)


"""
#根据北向排名列表里的代码名称，获取每一个代码的北向数据文件
#northflow_csv = stock_path + sp.get_separator() + "2021-02-10_northflow.csv"
northflow_csv = stock_path + sp.get_separator() + "dn" + sp.get_separator() + datestr + "_northflow.csv"
nf_df = pd.read_csv(northflow_csv,dtype={"SCode":str})
scode_df = nf_df["SCode"]
for i in range(scode_df.shape[0]):
  sc = scode_df[i]
  csv_name = stock_path + sp.get_separator() + "n" + sp.get_separator() + \
                                           sc + "_n.csv";
  try:
    stock_individual_fund_flow_df = ake.stock_em_hsgt_individual_stock_statistics(sc, datestr, datestr)
  except Exception as e:
    print(e)
    traceback.print_exc()
  if(stock_individual_fund_flow_df is None):
    continue
  #stock_individual_fund_flow_df.sort_values(by='持股日期', axis = 0, ascending=True,inplace = True)
  line = stock_individual_fund_flow_df.loc[cashintoday.shape[0]-1]  
 #col =['持股日期','股票代码','股票简称','当日收盘价','当日涨跌幅', '持股数量','持股数量占发行股百分比']
        ]]
  if(sc[0] == '2' or sc[0] == '9'):
    print(sc + " ignore")
    continue
  x = content.to_string(header=False,
                  index=False).split('\n')
  a=datestr + ',' + ','.join(v.strip() for v in x)
  csv_name =  stock_path + sp.get_separator() + "dn" +  sp.get_separator() + sc + "_n.csv"
  if not os.path.exists(csv_name):
     stock_individual_fund_flow_df.to_csv(csv_name, index=False)
     continue
  with open(csv_name,"a") as file:
    file.write(a + "\n")
    file.close()
"""
#获取今日北向排名
def get_north_rank():
  northflow_csv = stock_path + sp.get_separator() + "dn" + sp.get_separator() + datestr + "_n.csv"
  stock_em_hsgt_hold_stock_df = ake.stock_em_hsgt_hold_stock(market="北向", indicator="今日排行")
  stock_em_hsgt_hold_stock_df_s = stock_em_hsgt_hold_stock_df[["HdDate","SCode","SName","NewPrice","Zdf",
  "ShareHold","ShareSZ","LTSZ","LTZB","ZSZ","ZZB","ShareHold_Chg_One","ShareSZ_Chg_One","ShareSZ_Chg_Rate_One","LTZB_One","ZZB_One"]]
  stock_em_hsgt_hold_stock_df_s.to_csv(northflow_csv,index=False)

#根据当日北向排名列表里的代码名称及数据，追加单个股票的北向数据文件
def append_north_rank_to_each_stock():
  #northflow_csv = stock_path + sp.get_separator() + "2021-02-10_northflow.csv"
  northflow_csv = stock_path + sp.get_separator() + "dn" + sp.get_separator() + datestr + "_n.csv"
  nf_df = pd.read_csv(northflow_csv,dtype={"SCode":str})
  nf_df = nf_df[["HdDate","SCode","SName","NewPrice","Zdf",
  "ShareHold","ZZB"]]
  nf_df.loc[:,"ZZB"]*=100
  scode_df = nf_df["SCode"]
  for i in range(scode_df.shape[0]):
    sc = scode_df[i]
    content = nf_df.loc[i]
   #col =['持股日期','股票代码','股票简称','当日收盘价','当日涨跌幅', '持股数量','持股数量占发行股百分比']
  
    if(sc[0] == '2' or sc[0] == '9'):
      print(sc + " ignore")
      continue
    x = content.to_string(header=False,
                    index=False).split('\n')
    a=','.join(v.strip() for v in x)
    csv_name =  stock_path + sp.get_separator() + "n" +  sp.get_separator() + sc + "_n.csv"
    wh = False
    if not os.path.exists(csv_name):
       print(csv_name + " not exist,create it")
       wh = True
  
    with open(csv_name,"a",encoding="utf-8") as file:
      if(wh == True):
        file.write("持股日期,股票代码,股票简称,当日收盘价,当日涨跌幅,持股数量,持股数量占发行股百分比\n")
      file.write(a + "\n")
      file.close()

def get_stock_fundflow_his(sc:str):
  csv_name =  stock_path + sp.get_separator() + "m3" +  sp.get_separator() + sc + "_f.csv"
  if(sc[0]=='6'):
    mc = "sh"
  else:
    mc = "sz"
  stock_individual_fund_flow_df = ak.stock_individual_fund_flow(stock=sc, market=mc)
  stock_individual_fund_flow_df.to_csv(csv_name,index=False)

'''
#根据北向排名列表里的代码名称，获取每一个代码的北向数据文件删除最后一行
northflow_csv = stock_path + sp.get_separator() + "2021-02-10_northflow.csv"
nf_df = pd.read_csv(northflow_csv,dtype={"SCode":str})
scode_df = nf_df["SCode"]
for i in range(scode_df.shape[0]):
  sc = scode_df[i]
  csv_name = stock_path + sp.get_separator() + "n" + sp.get_separator() + \
                                           sc + "_n.csv";
  if not os.path.exists(csv_name):
    continue
  try:
    stock_individual_fund_flow_df = pd.read_csv(csv_name,dtype={"股票代码":str})
  except Exception as e:
    print(e)
    traceback.print_exc()
  if(stock_individual_fund_flow_df is None):
    continue
 # stock_individual_fund_flow_df.sort_values(by='持股日期', axis = 0, ascending=True,inplace = True)
  stock_individual_fund_flow_df = stock_individual_fund_flow_df.drop(index = stock_individual_fund_flow_df.shape[0]-1)
  stock_individual_fund_flow_df.to_csv(csv_name, index=False)
'''

'''
northflow_csv = stock_path + sp.get_separator() + "2021-02-10_northflow.csv"
nf_df = pd.read_csv(northflow_csv,dtype={"SCode":str})
scode_df = nf_df["SCode"]
for i in range(scode_df.shape[0]):
  sc = scode_df[i]
  csv_name = stock_path + sp.get_separator() + "n" + sp.get_separator() + \
                                           sc + "_n.csv";
  if not os.path.exists(csv_name):
    continue
  try:
    stock_individual_fund_flow_df = pd.read_csv(csv_name,dtype={"股票代码":str})
  except Exception as e:
    print(e)
    traceback.print_exc()
  if(stock_individual_fund_flow_df is None):
    continue
 # stock_individual_fund_flow_df.sort_values(by='持股日期', axis = 0, ascending=True,inplace = True)
  stock_individual_fund_flow_df = stock_individual_fund_flow_df.drop(index = stock_individual_fund_flow_df.shape[0]-1)
  stock_individual_fund_flow_df.to_csv(csv_name, index=False)
'''


#get_north_rank()

#append_north_rank_to_each_stock()

#get_fund_flow_rank()

#draw_fund_flow_rank_table()
#"沪股通", "深股通",

#get_stock_north_history("000059")


"""
stock_em_hsgt_hist_df = ake.stock_em_hsgt_hist(symbol="沪股通")
csv_name =  stock_path + sp.get_separator() + datestr + "_hgt.csv"
stock_em_hsgt_hist_df.to_csv(csv_name , index = False)

print(stock_em_hsgt_hist_df)

stock_em_hsgt_hist_df = ake.stock_em_hsgt_hist(symbol="深股通")
csv_name =  stock_path + sp.get_separator() + datestr + "_sgt.csv"
stock_em_hsgt_hist_df.to_csv(csv_name , index = False)
print(stock_em_hsgt_hist_df)
"""

def get_stock_comment():
  stock_em_comment_df = akc.stock_em_comment()
  #stock_em_comment_df.drop(stock_em_comment_df[(stock_em_comment_df.Focus=="-")|(stock_em_comment_df.Code.str.startswith("9"))|(stock_em_comment_df.Code.str.startswith("2"))].index,inplace=True)
  #stock_em_comment_df.drop(stock_em_comment_df[stock_em_comment_df.Focus=="-"].index,inplace=True)
  csv_name =  stock_path + sp.get_separator() + "c" +  sp.get_separator() + datestr + "_c.csv"
  stock_em_comment_df.to_csv(csv_name, index = False)

def convert_stock_comment(dates:str):
  csv_name =  stock_path + sp.get_separator() + "c" +  sp.get_separator() + dates + "_c.csv"
  stock_em_comment_df = pd.read_csv(csv_name,encoding="utf-8",dtype={"Code":str})
  stock_em_comment_df.drop(stock_em_comment_df[stock_em_comment_df.Focus=="-"].index,inplace=True)
  stock_em_comment_df.to_csv(csv_name, index = False)
  
def get_dfcf_focus_rank():
  df = fr.get_rank_list()
  if(df is not None):
    csv_name = stock_path + sp.get_separator() + "dr" +  sp.get_separator() + datetimestr + "_r.csv"
    df.to_csv(csv_name, index = False)

def get_org_top_board(start:str,end:str):
  df = tb.get_org_top_board(start,end)
  if(df is not None):
    csv_name = stock_path + sp.get_separator() + "dt" +  sp.get_separator() + start + "_t.csv"
    print("save org top board..."+ " to " + csv_name)
    df.to_csv(csv_name, index = False)
  else:
     print("get_org_top_board emypt for "+start + " to " + end)

def get_top_board(start:str,end:str):
  df = tb.get_top_board(start,end)
  if(df is not None):
    csv_name = stock_path + sp.get_separator() + "dl" +  sp.get_separator() + start + "_l.csv"
    print("save top board..."+ " to " + csv_name)
    df.to_csv(csv_name, index = False)
  else:
    print("get_top_board emypt for "+start + " to " + end)


if __name__ == "__main__":
  argv = sys.argv
  if(len(argv) ==1):
    print(argv[0]+ " useage:")
    print("   a, get all follows")
    print("   n, get north fund flow data")
    print("   m, get main fund flow data")
    print("   c, get stock comment of  dfcf")
    print("   r, draw fundlow rank table, if no args,draw the current day,else use date string like 2021-05-21 as param")
    print("   t, get dfcf long fu bang with fund originazation")
    print("   l, get dfcf long fu bang ")
    exit(0)

  if(argv[1]=="a"):
    n=len(argv)
    if(n ==3):
      datestr =argv[2]
      datetimestr = datestr + '-11-50'
      fundflow_csv = stock_path + sp.get_separator() + "dm" + sp.get_separator() + argv[2] + "_f.csv"
      fundflow_png = png_path + sp.get_separator() + argv[2] +"_fundflow.png"

    print("get_dfcf_focus_rank...")
    get_dfcf_focus_rank()

    print("get_fund_flow_rank...")
    get_fund_flow_rank()
    
    print("draw_fund_flow_rank_table...")
    draw_fund_flow_rank_table()
    
    print("get_stock_comment...")
    get_stock_comment()

    print("get_north_individual...")
    merge_north_individual()

    print("get_org_top_board...")
    get_org_top_board(datestr,datestr)

    print("get_top_board...")
    get_top_board(datestr,datestr)

  if(argv[1]=="n"):
    print("get_north_rank...")
    #get_north_rank()
    merge_north_individual()
    #get_north_individual("000700")
    #get_all_north_individual()



    #print("append_north_rank_to_each_stock...")
    #append_north_rank_to_each_stock()

  if(argv[1]=='m'):
    print("get_fund_flow_rank...")
    get_fund_flow_rank()
    print("draw_fund_flow_rank_table...")
    draw_fund_flow_rank_table()

  if(argv[1]=='r'):
    print("draw_fund_flow_rank_table...")
    n=len(argv)
    if(n==3):
      fundflow_csv = stock_path + sp.get_separator() + "dm" + sp.get_separator() + argv[2] + "_f.csv"
      fundflow_png = png_path + sp.get_separator() + argv[2] +"_fundflow.png"
    draw_fund_flow_rank_table()

  if(argv[1]=='c'):
    print("get_stock_comment...")
    get_stock_comment()
    #convert_stock_comment("2021-03-03")
    #convert_stock_comment("2021-03-04")

  if(argv[1]=='t'):
    print("get_org_top_board...")
    n=len(argv)
    if(n==2):
      get_org_top_board(datestr,datestr)
    elif(n ==3):
      get_org_top_board(argv[2],argv[2])
    elif(n == 4):
      get_org_top_board(argv[2],argv[3])
  
  if(argv[1]=='l'):
    print("get_top_board...")
    n=len(argv)
    if(n==2):
      get_top_board(datestr,datestr)
    elif(n ==3):
      get_top_board(argv[2],argv[2])
    elif(n == 4):
      get_top_board(argv[2],argv[3])

    #convert_stock_comment("2021-03-03")
    #convert_stock_comment("2021-03-04")

  if(argv[1]=='h'):
    stock_em_hsgt_north_acc_flow_in_df = ake.stock_em_hsgt_north_acc_flow_in(
        indicator="北上"
    )
    print(stock_em_hsgt_north_acc_flow_in_df)
    print("-------------")
    stock_em_hsgt_north_acc_flow_in_df = ake.stock_em_hsgt_north_net_flow_in(
        indicator="北上"
    )
    print(stock_em_hsgt_north_acc_flow_in_df)

  if(argv[1]=='d'):
    print("d")
    #make_daily_fundflow()
    #make_daily_northrank()
    #merge_north_individual()
    #remake_north_daily_rank("2021-04-01_n.csv","2021-03-31_n.csv")
    """
    sc = "002396"
    sm = "sz"
    csv_name =  stock_path + sp.get_separator() + "m" +  sp.get_separator() + sc + "_f.csv";
    if not os.path.exists(csv_name):
      stock_individual_fund_flow_df = ak.stock_individual_fund_flow(stock=sc, market=sm)
      if(not stock_individual_fund_flow_df is None):
        stock_individual_fund_flow_df.to_csv(csv_name,index=False)
    """
    """
    lastdate = "2021-03-09"
    dn_csv = stock_path + sp.get_separator() + "dn" +  sp.get_separator() + lastdate + "_n.csv"
    if not os.path.exists(dn_csv):
      print(dn_csv + "not exist,please make it at first,maybe use make_daily_northrank()")
    dn_df = pd.read_csv(dn_csv,dtype={"股票代码":str})
    dn_df.iloc[0,0] = "1+++1"
    dn_csv = stock_path + sp.get_separator() + "dn" +  sp.get_separator() + "2021-03-10" + "_n.csv"
    dn_df.to_csv(dn_csv,index = False)
    """


  print('--end')    
   
